package com.fxkj.cloud.drms.ui.listener;

import com.fxkj.cloud.dbs.common.event.stream.RemoteDeviceCreateEvent;
import com.fxkj.cloud.drms.application.DeviceApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.function.Consumer;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/3/18 13:35
 */
@Component
@Slf4j
public class RemoteDeviceCreateListener {

    private final DeviceApplicationService deviceApplicationService;

    public RemoteDeviceCreateListener(DeviceApplicationService deviceApplicationService) {
        this.deviceApplicationService = deviceApplicationService;
    }

    @Bean
    public Consumer<RemoteDeviceCreateEvent> deviceCreate() {
        return event -> {
            UUID deviceId = event.getDeviceId();
            UUID productId = event.getProductId();
            UUID organizationId = event.getOrganizationId();
            log.trace("监听到远程设备创建事件,组织id:{},产品id:{},设备:{},", deviceId, productId, organizationId);
            deviceApplicationService.createDevice(organizationId, productId, deviceId);
        };
    }
}
